package com.akuhome.resource.service;

import com.akuhome.resource.DO.RoomType;
import com.akuhome.resource.DO.RoomWay;
import com.akuhome.resource.dao.CommonDao;
import com.akuhome.resource.dto.Menu;
import com.akuhome.resource.enums.ResultEnum;
import com.akuhome.resource.exception.ResourceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommonService {

    @Autowired
    private CommonDao commonDao;

    public List<Menu> findAllMenu(){
        List<Menu> menuList = commonDao.findAllMenu();
        Map<Integer,Menu> menuMap = new HashMap<>();
        List<Menu> rootMenu = new ArrayList<>();
        menuList.forEach(e->{
            menuMap.put(e.getId(),e);
        });
        menuList.forEach(e->{
            if(e.getParentId() == null){
                rootMenu.add(e);
            }else{
                if(menuMap.containsKey(e.getParentId())){
                    menuMap.get(e.getParentId()).getSubMenu().add(e);
                }else{
                    throw new ResourceException(ResultEnum.PARENT_ID_ERROR);
                }
            }
        });
        return rootMenu;
    }

    public List<RoomType> findAllRoomType(){
        return commonDao.findAllRoomType();
    }

    public List<RoomWay> findAllRoomWay(){
        return commonDao.findAllRoomWay();
    }
}
